package HLLib.ui;

import HLCode.HLClass;
import HLCode.HLClassManager;
import HLCode.HLClassManager_H;
import HLCode.HLObject;
import HLLib.base.HLGraphics;
import HLLib.base.HLGraphics_H;
import HLLib.base.HLRectangle;
import HLLib.base.HLString;
import HLLib.control.HLTextPage;
import HLLib.io.HLFile;

public class HLUIControlTextPage extends HLUIControl implements HLGraphics_H {
    public HLClass GetClass(HLClassManager classManager) {
        return classManager.GetLibClass(HLClassManager_H.CLASS_GROUP_TYPE_LIB_UI, HLClassManager_H.LIB_CLASS_UI_UIControlTextPage);
    }

    public int GetInt(int index) {
        switch (index) {
        case 1: {
            return textColor;
        }
        case 2: {
            return textShadowColor;
        }
        case 3: {
            return textStyle;
        }
        case 4: {
            return textPosStyle;
        }
        case 5: {
            return fontSize;
        }
        case 6: {
            return lineHeight;
        }
        }
        return super.GetInt(index);
    }

    public void SetInt(int index, int value) {
        switch (index) {
        case 1: {
            textColor = value;
            return;
        }
        case 2: {
            textShadowColor = value;
            return;
        }
        case 3: {
            textStyle = value;
            return;
        }
        case 4: {
            textPosStyle = (byte) value;
            return;
        }
        case 5: {
            fontSize = (short) value;
            return;
        }
        case 6: {
            lineHeight = (short) value;
            return;
        }
        }
        super.SetInt(index, value);
    }

    public boolean GetBoolean(int index) {
        return super.GetBoolean(index);
    }

    public void SetBoolean(int index, boolean value) {
        super.SetBoolean(index, value);
    }

    public HLObject GetObject(int index) {
        switch (index) {
        case 0: {
            return text;
        }
        case 1: {
            return textPage;
        }
        }
        return super.GetObject(index);
    }

    public void SetObject(int index, HLObject value) {
        switch (index) {
        case 0: {
            text = (HLString) value;
            return;
        }
        case 1: {
            textPage = (HLTextPage) value;
            return;
        }
        }
        super.SetObject(index, value);
    }

    HLString oldText = new HLString();

    public HLString text;

    public int textColor;

    public int textShadowColor;

    public int textStyle;

    public byte textPosStyle;

    public short fontSize;

    public short lineHeight;

    public HLTextPage textPage;

    public HLUIControlTextPage() {
    }

    HLUIControlTextPage(HLUIFrame ownerFrame) {
        super(ownerFrame);
    }

    void Load(HLFile file) {
        super.Load(file);

        text = file.ReadString();

        switch (file.ReadInt8()) {
        case 1: {
            textStyle = TEXT_STYLE_SHADOW;
            break;
        }
        case 2: {
            textStyle = TEXT_STYLE_AROUND;
            break;
        }
        case 3: {
            textStyle = TEXT_STYLE_CROSS;
            break;
        }
        case 4: {
            textStyle = TEXT_STYLE_RIM;
            break;
        }
        default: {
            textStyle = TEXT_STYLE_NORMAL;
            break;
        }
        }

        textPosStyle = file.ReadInt8();
        fontSize = file.ReadInt16();
        fontSize = FONT_SIZE_SMALL;

        byte a = file.ReadInt8();
        byte r = file.ReadInt8();
        byte g = file.ReadInt8();
        byte b = file.ReadInt8();
        textColor = HLGraphics.ARGB(a, r, g, b);

        a = file.ReadInt8();
        r = file.ReadInt8();
        g = file.ReadInt8();
        b = file.ReadInt8();
        textShadowColor = HLGraphics.ARGB(a, r, g, b);

        lineHeight = file.ReadInt16();
    }

    void Render(HLGraphics g, int rectX, int rectY, int rectWidth, int rectHeight, int color) {
        HLRectangle area = GetArea(rectX, rectY, rectWidth, rectHeight);
        HLString tempText = ownerFrame.owner.ownerUISeedManager.GetLanguageTextByScript(text);
        if (!oldText.Equals(tempText) || textPage == null || textPage.width != area.width || textPage.height != area.height) {
            oldText = tempText;
            textPage = new HLTextPage();
            textPage.SetSize(area.width, area.height);
            textPage.SettingNormal(lineHeight, 0, 0, 0, textColor, textShadowColor, textStyle, textPosStyle, fontSize);
            textPage.SetText(tempText);
        }
//        textPage.Logic();
        textPage.color = color;
        textPage.Render(g, area.x, area.y);
    }

    void CopyFrom(HLUIControl uiControl) {
        super.CopyFrom(uiControl);

        HLUIControlTextPage uiControlByType = (HLUIControlTextPage) uiControl;

        text = uiControlByType.text;
        textColor = uiControlByType.textColor;
        textShadowColor = uiControlByType.textShadowColor;
        textStyle = uiControlByType.textStyle;
        textPosStyle = uiControlByType.textPosStyle;

        fontSize = uiControlByType.fontSize;

        lineHeight = uiControlByType.lineHeight;

        textPage = null;
    }

    HLUIControl Clone(HLUIFrame ownerFrame) {
        HLUIControlTextPage uiControl = new HLUIControlTextPage();
        uiControl.ownerFrame = ownerFrame;
        uiControl.CopyFrom(this);
        return uiControl;
    }
}
